package com.csnf.dao.impl;

import com.csnf.dao.IStudentDao;
import com.csnf.entity.Student;
import com.csnf.util.SqlSessionUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * StudentDaoImpl 类实现了 IStudentDao 接口，提供了对学生数据的基本操作。
 */
public class StudentDaoImpl implements IStudentDao {
    //    /**
//     * 获取所有学生信息的方法。
//     *
//     * @return 包含所有学生信息的列表
//     * @throws IOException 如果读取 mybatis-config.xml 文件时发生错误
//     */
//    @Override
//    public List<Student> findAll() throws IOException {
//        // 定义 MyBatis 配置文件的路径
//        String path = "mybatis-config.xml";
//        // 从类路径中读取 MyBatis 配置文件
//        InputStream resourceAsStream=Resources.getResourceAsStream(path);
//        // 创建 SqlSessionFactoryBuilder 实例
//        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//        // 使用配置文件构建 SqlSessionFactory
//        SqlSessionFactory sqlSessionFactory = builder.build(resourceAsStream);
//        // 定义一个学生列表用于存储查询结果
//        List<Student> stuList;
//
//        // 使用 try-with-resources 语句确保 SqlSession 在使用后自动关闭
//        try (SqlSession session = sqlSessionFactory.openSession()) {
//            // 执行 SQL 查询，获取所有学生信息
//            stuList = session.selectList("com.csnf.dao.IStudentDao.findAll");
//        }
//        // 返回查询结果
//        return stuList;
////        SqlSession sqlSession = sqlSessionFactory.openSession();
////        return sqlSession;
//    }

    @Override
    public List<Student> findAll() throws IOException {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        List<Student> stuList = sqlSession.selectList("com.csnf.dao.IStudentDao.findAll");
        sqlSession.close();
        return stuList;
    }
    @Override
    public Student findById(Integer id) throws IOException {
        SqlSession sqlSession = getSqlSession();
        Student stu = sqlSession.selectOne("com.csnf.dao.IStudentDao.findById",id);
        sqlSession.close();
        return stu;
    }

    //实现通过姓名查询学生信息
    @Override
    public List<Student> findByName(String name) throws IOException {
        SqlSession sqlSession = getSqlSession();
        List<Student> stuList = sqlSession.selectList("com.csnf.dao.IStudentDao.findByName",name);
        sqlSession.close();
        return stuList;
    }

    @Override
    public void deleteById(Integer id) throws IOException {
        SqlSession sqlSession = getSqlSession();
        sqlSession.delete("com.csnf.dao.IStudentDao.deleteById",id);
        sqlSession.commit();// 提交事务
        sqlSession.close();
    }

    @Override
    public void update(Student student) throws IOException {
        SqlSession sqlSession =getSqlSession();
        sqlSession.update("com.csnf.dao.IStudentDao.update",student);
        sqlSession.commit();// 提交事务
        sqlSession.close();
    }

    @Override
    public void insert(Student student) throws IOException {
        SqlSession sqlSession = getSqlSession();
        sqlSession.insert("com.csnf.dao.IStudentDao.add",student);
        sqlSession.commit();// 提交事务
        sqlSession.close();
    }

    private SqlSession getSqlSession() throws IOException {
        // 定义 MyBatis 配置文件的路径
        String path = "mybatis-config.xml";
        // 从类路径中读取 MyBatis 配置文件
        InputStream resourceAsStream=Resources.getResourceAsStream(path);
        // 创建 SqlSessionFactoryBuilder 实例
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        // 使用配置文件构建 SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = builder.build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}